# ======================================================================
# Shared Functions
# File: functions/reshape_conjoint.R
# ======================================================================

reshape_conjoint <- function(.data, .attribute)
{
  
  out <- .data %>%
    select(ResponseId, contains(.attribute))
  
  profile1 <- out %>%
    select(ResponseId, matches("\\-1$")) %>%
    pivot_longer(cols = 2:ncol(.)) %>%
    mutate(task = str_extract(name, "(?<=\\w\\-)\\d+") %>% as.numeric(),
           profile = 1) %>%
    select(ResponseId, task, profile, value)
  
  profile2 <- out %>%
    select(ResponseId, matches("\\-2$")) %>%
    pivot_longer(cols = 2:ncol(.)) %>%
    mutate(task = str_extract(name, "(?<=\\w\\-)\\d+") %>% as.numeric(),
           profile = 2) %>%
    select(ResponseId, task, profile, value)

  out2 <- bind_rows(profile1, profile2) %>%
    set_names(c("ResponseId", "task", "profile", .attribute))
  
}